unit uMaquinas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, Buttons, StdCtrls, ToolWin, ComCtrls, ExtCtrls,
  TeEngine, Series, BubbleCh, TeeProcs, Chart, IBQuery, ActnList, System.Actions,
  Vcl.Menus;

type
  TMaquinas_F = class(TForm)
    tbTOP: TToolBar;
    info_Top: TLabel;
    sbNovo: TSpeedButton;
    sbPesq: TSpeedButton;
    spDel: TSpeedButton;
    spGrava: TSpeedButton;
    spSair: TSpeedButton;
    tbTopDesc: TToolBar;
    Info_RazaoSocial: TLabel;
    tbMensagem: TToolBar;
    lblMensagem: TLabel;
    GridMaquinas: TDBGrid;
    DsMaquinas: TDataSource;
    Chart1: TChart;
    Series1: TBubbleSeries;
    Timer_Open: TTimer;
    ActionList1: TActionList;
    Sair: TAction;
    Novo: TAction;
    acNewByIP: TAction;
    popMaquinas: TPopupMenu;
    FerramentasMquinas1: TMenuItem;
    popMaq_AtualizarSupportTi: TMenuItem;
    acUpdSupportTi: TAction;
    sbTools: TSpeedButton;
    acPopTools: TAction;
    acResetSupportTI: TAction;
    esteComando1: TMenuItem;
    acUpdQuery: TAction;
    Action1: TAction;
    acInstUpdSupportTI: TAction;
    N2ResetStartSupportTI1: TMenuItem;
    teste: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure SairExecute(Sender: TObject);
    procedure Timer_OpenTimer(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure sbToolsClick(Sender: TObject);
    procedure acPopToolsExecute(Sender: TObject);
    procedure acNewByIPExecute(Sender: TObject);
    procedure acResetSupportTIExecute(Sender: TObject);
    procedure acUpdQueryExecute(Sender: TObject);
    procedure acInstUpdSupportTIExecute(Sender: TObject);
    procedure GridMaquinasTitleClick(Column: TColumn);
    procedure GridMaquinasKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure GridMaquinasCellClick(Column: TColumn);
    procedure GridMaquinasKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);

  private
    procedure SelecionaIPGrid;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Maquinas_F: TMaquinas_F;
  vfOrderTitle : String;

implementation

uses
  mdsrv_u, uMaqFunctions, uFunctions, mdtemp_u, uDiag_IpRange, gVars_u, synaip;

{$R *.dfm}

procedure TMaquinas_F.acInstUpdSupportTIExecute(Sender: TObject);
var
i:Integer;
vpIP:String;
vpBookNumber: TBookmark;
begin

  if GridMaquinas.SelectedRows.Count>0 then
    Begin
      vpBookNumber :=(GridMaquinas.Datasource.Dataset.Bookmark);
      GridMaquinas.Datasource.Dataset.DisableControls;
      for I := 0 to GridMaquinas.SelectedRows.Count - 1 do
        Begin
          GridMaquinas.Datasource.Dataset.Bookmark := GridMaquinas.SelectedRows[i];

        End;

      GridMaquinas.Datasource.Dataset.Bookmark := vpBookNumber;
      GridMaquinas.Datasource.Dataset.EnableControls;
      ExecAndWait ('C:\SUPPORTTI\PSTOOLS\psexec.exe',' \\'+ GridMaquinas.Datasource.Dataset.FieldByName('MAQ_IPATUAL').AsString+' -u granderio\administrador -p d13go@1996 cmd /c "call \\10.10.10.8\version\upd.bat"',SW_NORMAL);
      exit;
    End;



  Diag_IpRange_F := TDiag_IpRange_F.Create(Application);
    if Diag_IpRange_F.ShowModal = 2  then
      Exit;

    for I := StrToIp(vgFiltro.IP_Inicial) to StrToIP(vgFiltro.IP_Final) do
      Begin
        vpIP := IntToIpP(i);
        //WinExec(PAnsiChar('C:\SUPPORTTI\PSTOOLS\psexec.exe \\'+vpIp+' -i taskkill -f -im SupportTI.exe'),SW_NORMAL);
        //WinExec(PAnsiChar('C:\SUPPORTTI\PSTOOLS\psexec.exe \\'+vpIp+' -i c:\supportti\supportti.exe') ,SW_NORMAL);

        ExecAndWait ('C:\SupportTI\PSTools\psexec.exe',' \\'+vpIP+' -u granderio\administrador -p d13go@1996 cmd /c "call \\10.10.10.8\version\upd.bat"',SW_NORMAL);
//        ExecAndWait ('C:\SupportTI\PSTools\psexec.exe',' \\'+vpIP+' granderio\administrador -p d13go@1996 -i -d c:\supportti\supportti.exe',SW_NORMAL);
//        ExecAndWait ('taskkill.exe',' -f -im psexec.exe',SW_NORMAL);

      end;


end;

procedure TMaquinas_F.acNewByIPExecute(Sender: TObject);
begin
fcMaq_SupportTI_Install;
end;

procedure TMaquinas_F.acPopToolsExecute(Sender: TObject);
begin

   popMaquinas.Popup(Self.Left+250,Self.Top+150);

end;

procedure TMaquinas_F.acResetSupportTIExecute(Sender: TObject);
var
i:Integer;
vpIP:String;
vpBookNumber: TBookmark;
begin

  if GridMaquinas.SelectedRows.Count>0 then
    Begin
      vpBookNumber :=(GridMaquinas.Datasource.Dataset.Bookmark);
      GridMaquinas.Datasource.Dataset.DisableControls;
      for I := 0 to GridMaquinas.SelectedRows.Count - 1 do
        Begin
          GridMaquinas.Datasource.Dataset.Bookmark := GridMaquinas.SelectedRows[i];

        End;

      GridMaquinas.Datasource.Dataset.Bookmark := vpBookNumber;
      GridMaquinas.Datasource.Dataset.EnableControls;
      ExecAndWait ('C:\SUPPORTTI\PSTOOLS\psexec.exe',' \\'+ GridMaquinas.Datasource.Dataset.FieldByName('MAQ_IPATUAL').AsString+' -u granderio\administrador -p d13go@1996 cmd /c "call \\10.10.10.8\version\Start.bat"',SW_NORMAL); //taskkill -f -im SupportTI.exe',SW_NORMAL);
      ExecAndWait ('C:\SupportTI\PSTools\psexec.exe',' \\'+ GridMaquinas.Datasource.Dataset.FieldByName('MAQ_IPATUAL').AsString+' -u granderio\administrador -p d13go@1996 -i -d c:\supportti\supportti.exe',SW_NORMAL);

      exit;
    End;


  Diag_IpRange_F := TDiag_IpRange_F.Create(Application);
    if Diag_IpRange_F.ShowModal = 2  then
      Exit;



    for I := StrToIp(vgFiltro.IP_Inicial) to StrToIP(vgFiltro.IP_Final) do
      Begin
        vpIP := IntToIpP(i);
        WinExec(PAnsiChar ('C:\SUPPORTTI\PSTOOLS\psexec.exe \\'+vpIp+' -u granderio\administrador -p d13go@1996 cmd /c "call \\10.10.10.8\version\Start.bat"'),SW_NORMAL); //taskkill -f -im SupportTI.exe',SW_NORMAL);
        WinExec(PAnsiChar ('C:\SupportTI\PSTools\psexec.exe \\'+vpIP+' -u granderio\administrador -p d13go@1996 -i -d c:\supportti\supportti.exe'),SW_NORMAL);
        Timer_Open.Enabled := True;

      end;




end;

procedure TMaquinas_F.acUpdQueryExecute(Sender: TObject);
begin
   Timer_Open.Enabled := True;
end;

procedure TMaquinas_F.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := CaFree;
  Maquinas_F := nil;
end;

procedure TMaquinas_F.FormCreate(Sender: TObject);
begin
 DsMaquinas.DataSet := mdtemp.QdsMaquinas;
end;

procedure TMaquinas_F.FormShow(Sender: TObject);
begin
   Timer_Open.Enabled := True;
end;

procedure TMaquinas_F.GridMaquinasCellClick(Column: TColumn);
begin
   SelecionaIPGrid;
end;

procedure TMaquinas_F.GridMaquinasKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_SPACE then
    Begin
      (Sender as TDbGrid).SelectedRows.CurrentRowSelected  := not (Sender as TDbGrid).SelectedRows.CurrentRowSelected;
      (Sender as TDbGrid).DataSource.DataSet.Next;
      exit;
    end;

  if key = VK_BACK then
    Begin
      (Sender as TDbGrid).SelectedRows.Clear;
      exit;
    end;



end;

procedure TMaquinas_F.GridMaquinasKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  SelecionaIPGrid
end;

procedure TMaquinas_F.GridMaquinasTitleClick(Column: TColumn);
begin

  ReConnectLocal;
  if vfOrderTitle=Column.FieldName then
    vfOrderTitle := Column.FieldName +' DESC'
  else
    vfOrderTitle := Column.FieldName;


  mdtemp.QdsMaquinas.Close;
  mdtemp.QdsMaquinas.SelectSQL.Text:= 'SELECT MAQ_ID, MAQ_IPATUAL, MAQ_IPANT, MAQ_IPMACATUAL, MAQ_IPMACANT, MAQ_NOME, MAQ_DOMINIO, MAQ_DTCAD, MAQ_DTUPD,  '+
                                     'MAQ_FUNCAO, MAQ_USADOPOR, MAQ_USER, MAQ_USERATIVO, MAQ_SO, MAQ_STATUS, MAQ_CPUUSO, MAQ_CPUCLOCK, MAQ_MEMUSO,  '+
                                     'MAQ_MEMQTD, MAQ_DESCRICAO, MAQ_ULTERROR, MAQ_UPDPULSER, '+
                                     'CASE WHEN ( CURRENT_TIMESTAMP - MAQ_UPDPULSER) * 1440 < 5 THEN ''On'' ELSE ''Off'' '+
                                         'END AS XXST FROM TEMPMAQUINAS ORDER BY '+vfOrderTitle;
  mdtemp.QdsMaquinas.Prepare;
  mdtemp.QdsMaquinas.Open;


end;

procedure TMaquinas_F.SairExecute(Sender: TObject);
begin
  Self.Close;
end;

procedure TMaquinas_F.sbToolsClick(Sender: TObject);
begin
   acPopTools.Execute;

end;

procedure TMaquinas_F.SelecionaIPGrid;
begin
  if not  GridMaquinas.DataSource.DataSet.Active then
    exit;

  vgFiltro.IP_Inicial := GridMaquinas.Datasource.Dataset.FieldByName('MAQ_IPATUAL').AsString;
  vgFiltro.IP_Final   := GridMaquinas.Datasource.Dataset.FieldByName('MAQ_IPATUAL').AsString;

end;

procedure TMaquinas_F.Timer_OpenTimer(Sender: TObject);
var
vpQueryServidor: TIBQuery;
begin
  (Sender as TTImer).Enabled := False;

  ReConnectServ;

  vpQueryServidor := mdsrv.Qry;
  vpQueryServidor.Close;
  vpQueryServidor.SQL.Text := 'SELECT * FROM MAQUINAS ORDER BY MAQ_IPATUAL';
  vpQueryServidor.Prepare;
  vpQueryServidor.Open;


  IniciaMaquina;
  vpQueryServidor.First;
  while not vpQueryServidor.eof do
    Begin
      Executa_Script_Local_QryScript(CopyAsInsert(vpQueryServidor,'TEMPMAQUINAS'));
      vpQueryServidor.Next;
    end;
  ReConnectLocal;
  vfOrderTitle := 'XXST DESC,MAQ_IPATUAL';
  mdtemp.QdsMaquinas.Close;
  mdtemp.QdsMaquinas.SelectSQL.Text:= 'SELECT MAQ_ID, MAQ_IPATUAL, MAQ_IPANT, MAQ_IPMACATUAL, MAQ_IPMACANT, MAQ_NOME, MAQ_DOMINIO, MAQ_DTCAD, MAQ_DTUPD,  '+
                                     'MAQ_FUNCAO, MAQ_USADOPOR, MAQ_USER, MAQ_USERATIVO, MAQ_SO, MAQ_STATUS, MAQ_CPUUSO, MAQ_CPUCLOCK, MAQ_MEMUSO,  '+
                                     'MAQ_MEMQTD, MAQ_DESCRICAO, MAQ_ULTERROR, MAQ_UPDPULSER, '+
                                     'CASE WHEN ( CURRENT_TIMESTAMP - MAQ_UPDPULSER) * 1440 < 5 THEN ''On'' ELSE ''Off'' '+
                                         'END AS XXST FROM TEMPMAQUINAS ORDER BY '+vfOrderTitle;
  mdtemp.QdsMaquinas.Prepare;
  mdtemp.QdsMaquinas.Open;



end;

end.




